package service.review;

import java.util.List;

import service.notice.NoticeService;
import util.manager.MajorunitManager;
import util.manager.PageManager;
import util.paramvo.PageListParamVO;
import util.returnvo.ReviewListReturnVO;
import vo.MajorunitVO;
import vo.ReviewVO;
import config.PageConfig;
import dao.review.ReviewDAO;

public class ReviewServiceImpl implements ReviewService{

	private ReviewDAO reviewDAO;
	private NoticeService noticeService;
	
	public void setReviewDAO(ReviewDAO reviewDAO) {
		this.reviewDAO = reviewDAO;
	}
	
	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}
	
	@Override
	public ReviewListReturnVO getUnapprovalReviewList(String page) throws Exception {
		
		int pageNo=1;
		
		if(page!=null && !page.equals("")) pageNo=Integer.parseInt(page);
		
		List<ReviewVO> ReviewList=reviewDAO.getUnapprovalReviewList(new PageListParamVO(PageConfig.REVIEW_ROW_PER_PAGE, pageNo));
		
		return new ReviewListReturnVO(ReviewList, new PageManager(PageConfig.REVIEW_ROW_PER_PAGE, PageConfig.REVIEW_PAGE_PER_GROUP, reviewDAO.countUnapprovalReviewList(), pageNo));
	}


	@Override
	public void approveReview(int review_no) throws Exception {
		reviewDAO.approveReview(new ReviewVO(review_no, 1));
		
		ReviewVO review=reviewDAO.getReview(review_no);
		MajorunitVO majorunit=review.getMajorunit();
		noticeService.toInterestedMember(
				majorunit.getMajorunit_no(), 
				"새 학과리뷰", 
				"<a href='review.do?command=examinfo&&majorunit_no="+majorunit.getMajorunit_no()+"'>"+MajorunitManager.convertUnitToString(majorunit)+"</a>에 새 학과리뷰가 등록되었습니다.");
		
		noticeService.sendNotice(
				review.getMember().getMember_no(), 
				"승인 완료", 
				"<a href='review.do?command=review&&majorunit_no="+majorunit.getMajorunit_no()+"'>"+MajorunitManager.convertUnitToString(majorunit)+"</a>에 작성하신 "+review.getReview_title()+" 글이 승인 완료되었습니다.");
	}

	@Override
	public void refuseReview(int review_no, String reason) throws Exception {
		reviewDAO.approveReview(new ReviewVO(review_no, -1));
		
		ReviewVO review=reviewDAO.getReview(review_no);
		MajorunitVO majorunit=review.getMajorunit();
		noticeService.sendNotice(
				review.getMember().getMember_no(), 
				"승인 불가", 
				"<a href='review.do?command=review&&majorunit_no="+majorunit.getMajorunit_no()+"'>"+MajorunitManager.convertUnitToString(majorunit)+"</a>에 작성하신 "+review.getReview_title()+" 글이 승인 불가 판정을 받았습니다.<br>"+
				"승인 불가이유 : <br>"+reason);
	}
}
